url = "https://raw.githubusercontent.com/MicrosoftDocs/mslearn-introduction-to-machine-learning/main/Data/ml-basics/penguins.csv"
import pandas as pd

# 正确的 URL
url = "https://raw.githubusercontent.com/MicrosoftDocs/mslearn-introduction-to-machine-learning/main/Data/ml-basics/penguins.csv"
df = pd.read_csv(url)
print(df.head())
import matplotlib.pyplot as plt

# 企鹅种类的分布条形图
df['Species'].value_counts().plot(kind='bar')
plt.title('Penguin Species Distribution')
plt.xlabel('Species')
plt.ylabel('Count')
plt.show()
import seaborn as sns

# 箱线图
sns.boxplot(data=df, x='Species', y='FlipperLength')
plt.title('Flipper Length by Species')
plt.show()

sns.boxplot(data=df, x='Species', y='CulmenLength')
plt.title('Culmen Length by Species')
plt.show()

sns.boxplot(data=df, x='Species', y='CulmenDepth')
plt.title('Culmen Depth by Species')
plt.show()
print(df[df.isnull().any(axis=1)])
df = df.dropna()
from sklearn.model_selection import train_test_split

# 特征和标签
features = ['CulmenLength', 'CulmenDepth', 'FlipperLength']
labels = 'Species'

# 分割数据集
X = df[features]
y = df[labels]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
from sklearn.linear_model import LogisticRegression

# 创建多类逻辑回归模型
model = LogisticRegression(multi_class='multinomial', solver='lbfgs')

# 训练模型
model.fit(X_train, y_train)
from sklearn.metrics import accuracy_score

# 预测测试集标签
y_pred = model.predict(X_test)

# 计算模型准确率
accuracy = accuracy_score(y_test, y_pred)
print(f'Model accuracy: {accuracy:.2f}')